﻿/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * Author: Laurent WOUTERS
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace xowl.gmi.compiler
{
    public class Program
    {
        private static log4net.ILog logger;

        public static log4net.ILog Logger { get { return logger; } }

        static void Main(string[] args)
        {
            Program program = new Program();
            program.Execute(args);
        }

        private string workbench;
        private List<string> vFiles;
        private List<string> tFiles;

        public Program()
        {
            log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("%-5p: %m%n");
            log4net.Appender.ConsoleAppender appender = new log4net.Appender.ConsoleAppender();
            appender.Layout = layout;
            log4net.Config.BasicConfigurator.Configure(appender);
            logger = log4net.LogManager.GetLogger(typeof(Program));
        }

        public void Execute(string[] args)
        {
            this.workbench = null;
            this.vFiles = new List<string>();
            this.tFiles = new List<string>();
            if (args.Length == 0)
            {
                logger.Fatal("No argument provided!");
                return;
            }
            LoadArguments(args);
            if (workbench == null)
            {
                logger.Fatal("Workbench name is indefined!");
                return;
            }
            if (vFiles.Count == 0 && tFiles.Count == 0)
            {
                logger.Fatal("No input file specified!");
                return;
            }
            Compiler compiler = new Compiler(workbench);
            foreach (string view in vFiles)
                compiler.LoadView(view);
            foreach (string transform in tFiles)
                compiler.LoadTransform(transform);
            compiler.Compile();
        }

        private void LoadArguments(string[] args)
        {
            for (int i = 0; i != args.Length; i++)
            {
                string arg = args[i];
                if (arg.StartsWith("\"") && arg.EndsWith("\""))
                    arg = arg.Substring(1, arg.Length - 2);
                if (arg.EndsWith(".view"))
                    vFiles.Add(arg);
                else if (arg.EndsWith(".transform"))
                    tFiles.Add(arg);
                else if (workbench == null)
                    workbench = arg;
                else
                {
                    logger.Warn("Multiple definition of the workbench, old value: " + workbench + ", new value: " + arg);
                    workbench = arg;
                }
            }
        }
    }
}
